package cn.uppower.wordaliy.activity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.text.InputType;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

import cn.uppower.wordaliy.R;
import cn.uppower.wordaliy.WordaliyApplication;
import cn.uppower.wordaliy.widget.BaseActivity;

/**
 * Created by elitetyc on 2018/12/14.
 * Describe:
 */
public class SettingActivity extends BaseActivity {


    private List<String> settings;

    private ListView lv_setting;



    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initData();
    }

    private void initData() {
        settings=new ArrayList<>();
        settings.add("单词错误重复出现个数");
        settings.add("测试单词个数");
        lv_setting.setAdapter(new MySettingAdapter());
        lv_setting.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                switch (position){
                    case 0:
                        final EditText editText = new EditText(SettingActivity.this);
                        editText.setInputType(InputType.TYPE_CLASS_NUMBER);
                        editText.setMaxLines(1);
                        AlertDialog.Builder inputDialog= new AlertDialog.Builder(SettingActivity.this);
                        inputDialog.setTitle("单词错误后多少个开始重复出现").setView(editText);
                        inputDialog.setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        int number=10;
                                        try{
                                            number = Integer.parseInt(editText.getText().toString().trim());
                                        }catch (Exception e){
                                            e.printStackTrace();
                                            Toast.makeText(SettingActivity.this, "输入错误,系统设置为默认值10", Toast.LENGTH_SHORT).show();
                                            number=10;
                                        }finally {
                                            WordaliyApplication.wordaliyEditor.putInt("NumberOfRepeatedWords",number);
                                            WordaliyApplication.wordaliyEditor.commit();
                                        }


                                    }
                                }).show();
                        break;
                    case 1:
                        final EditText editText1 = new EditText(SettingActivity.this);
                        editText1.setInputType(InputType.TYPE_CLASS_NUMBER);
                        editText1.setMaxLines(1);
                        AlertDialog.Builder inputDialog1= new AlertDialog.Builder(SettingActivity.this);
                        inputDialog1.setTitle("请输入测试单词个数").setView(editText1);
                        inputDialog1.setPositiveButton("确定",
                                new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        int number=24;
                                        try{
                                            number = Integer.parseInt(editText1.getText().toString().trim());
                                            if (number>24){
                                                Toast.makeText(SettingActivity.this, "数字大于词库单词数，设置为默认值24", Toast.LENGTH_SHORT).show();
                                                number=24;
                                            }

                                            if (number<0){
                                                Toast.makeText(SettingActivity.this, "输入数字小于0，设置为默认值24", Toast.LENGTH_SHORT).show();
                                            }
                                        }catch (Exception e){
                                            e.printStackTrace();
                                            Toast.makeText(SettingActivity.this, "输入错误,系统设置为默认值24", Toast.LENGTH_SHORT).show();
                                            number=24;
                                        }finally {
                                            WordaliyApplication.wordaliyEditor.putInt("TestNumberOfWords",number);
                                            WordaliyApplication.wordaliyEditor.commit();
                                        }


                                    }
                                }).show();
                        break;
                    case 2:
                        break;
                }
            }
        });
    }


    @Override
    public Integer getLayoutResId() {
        return R.layout.activity_setting;
    }

    @Override
    public void init() {

        lv_setting = findViewById(R.id.lv_setting);

    }


    class MySettingAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return settings.size();
        }

        @Override
        public Object getItem(int position) {
            return settings.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            SettingViewHolder settingViewHolder=null;
            if (convertView==null){
                settingViewHolder=new SettingViewHolder();
                convertView= View.inflate(SettingActivity.this,R.layout.item_setting,null);
                settingViewHolder.tv_text = convertView.findViewById(R.id.tv_text);
                convertView.setTag(settingViewHolder);
            }
            settingViewHolder.tv_text.setText(settings.get(position));
            return convertView;
        }


        class SettingViewHolder{
            TextView tv_text;

        }
    }
}
